package com.itliang.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itliang.constant.RedisConstants;
import com.itliang.core.model.dto.system.sysuser.SysUserUpdateStatusRequest;
import com.itliang.core.model.entity.system.SysUser;
import com.itliang.core.model.vo.RouterVo;
import com.itliang.core.redis.RedisCache;
import com.itliang.mapper.system.SysUserMapper;
import com.itliang.service.system.SysMenuService;
import com.itliang.service.system.SysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 用户服务
 *
 * @author liangzhuoyuan
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private RedisCache redisCache;


    /**
     * 更新用户状态
     *
     * @param updateStatusRequest
     * @return
     */
    @Override
    public boolean updateStatus(SysUserUpdateStatusRequest updateStatusRequest) {
        SysUser sysUser = sysUserMapper.selectById(updateStatusRequest.getId());
        sysUser.setStatus(updateStatusRequest.getStatus());
        int result = sysUserMapper.updateById(sysUser);
        return result == 1;
    }

    @Override
    public SysUser getByUserAccount(String userAccount) {
        return sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("user_account", userAccount));
    }

    /**
     * 获取用户权限信息
     *
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> getUserInfo(Long userId) {
        Map<String, Object> resultMap = new HashMap<>();
        String redisKey = RedisConstants.USER_INFO_KEY + getUSerInfoById(userId).getUserAccount();
        Boolean hasKey = redisCache.hasKey(redisKey);
        if (Boolean.TRUE.equals(hasKey)) {
            redisCache.expire(redisKey, RedisConstants.USER_INFO_TTL);
            resultMap = redisCache.getCacheObject(redisKey);
        } else {
            // 根据userId查询菜单权限值
            List<RouterVo> routerVoList = sysMenuService.getUserMenuList(userId);
            // 根据userId查询按钮权限值
            List<String> permsList = sysMenuService.getUserButtonList(userId);

            resultMap.put("roles", "[\"admin\"]");
            resultMap.put("name", "admin");
            resultMap.put("avatar", "https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
            resultMap.put("routers", routerVoList);
            resultMap.put("buttons", permsList);
            redisCache.setCacheObject(redisKey, resultMap, RedisConstants.USER_INFO_TTL, TimeUnit.SECONDS);
        }

        return resultMap;
    }

    public SysUser getUSerInfoById(Long userId) {
        return sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("id", userId));
    }

}




